var _loginHtml = '<div class="mine-btn-wrap" id="loginDialog">' +
    '<div class="tit">' +
    '<p class="title">登录</p>' +
    '<p class="tips">欢迎您登录小清柠</p>' +
    '</div>' +
    '<div class="dialog_input">' +
    '<div class="input_d"><input type="text" id="mobile" placeholder="手机号" /></div>' +
    '</div>' +
    '<div class="dialog_input">' +
    '<div class="input_d"><input type="password" id="password" placeholder="密码" /></div>' +
    '</div>' +
    '<button type="button" class="confirmBtn" >' +
    '<span style="font-size: 16px;">登录</span>' +
    '</button>' +
    '<p class="tips_bottom left" id="register">注册账号</p><p class="tips_bottom right" id="forget">忘记密码？</p>' +
    '<div class="bottom"><img style="width: 40px;height: 40px;" src="app/images/wx_logo.png"><p>快捷登录</p></div>',
    _loginWxHtml = '<div class="mine-btn-wrap" id="wxLoginDialog">' +
        '<div class="tit" style="text-align: center;">' +
        '<p class="tips" style="color: #5C5C5C;padding-bottom: 20px;">离成功还差登录</p>' +
        '</div>' +
        '<button type="button" class="confirmBtn" style="margin-top: 0;width: 90% !important;">' +
        '<img style="width: 20px;height: 20px;display: inline-block;vertical-align: middle;" src="app/images/wx_logo.png">' +
        '<span style="font-size: 16px;display: inline-block;vertical-align: middle;padding-left: 10px;">微信登录</span>' +
        '</button>' +
        '<div class="bottom" style="bottom: 15px;"><p>-------------------------------------------------</p><p id="otherLogin">其他登录方式</p></div>',
    _registerHtml = '<div class="mine-btn-wrap" id="registerDialog">' +
        '<div class="tit">' +
        '<p class="title">注册</p>' +
        '<p class="tips">欢迎您注册小清柠</p>' +
        '</div>' +
        '<div class="dialog_input">' +
        '<div class="input_d"><input type="text" id="mobile" placeholder="手机号" /></div>' +
        '</div>' +
        '<div class="dialog_input">' +
        '<div class="input_d w70"><input type="text" id="verifyCode" placeholder="验证码" /></div>' +
        '<a class="verify green">获取验证码</a>' +
        '</div>' +
        '<div class="dialog_input">' +
        '<div class="input_d"><input type="password" id="password" placeholder="密码" /></div>' +
        '</div>' +
        '<div class="dialog_input">' +
        '<div class="input_d"><input type="password" id="re_pwd" placeholder="重复密码" /></div>' +
        '</div>' +
        '<button type="button" class="confirmBtn" >' +
        '<span style="font-size: 16px;">确认</span>' +
        '</button>' +
        '<p class="tips_bottom right" id="login">登录已有账号</p>',
    _forgetHtml = '<div class="mine-btn-wrap" id="forgetDialog">' +
        '<div class="tit">' +
        '<p class="title">重置密码</p>' +
        '<p class="tips">使用已绑定的手机号重置密码</p>' +
        '</div>' +
        '<div class="dialog_input">' +
        '<div class="input_d"><input type="text" id="mobile" placeholder="手机号" /></div>' +
        '</div>' +
        '<div class="dialog_input">' +
        '<div class="input_d w70"><input type="text" id="verifyCode" placeholder="验证码" /></div><a class="verify green">获取验证码</a>' +
        '</div>' +
        '<div class="dialog_input">' +
        '<div class="input_d"><input type="password" id="password" placeholder="新密码" /></div>' +
        '</div>' +
        '<div class="dialog_input">' +
        '<div class="input_d"><input type="password" id="re_pwd" placeholder="确认密码" /></div>' +
        '</div>' +
        '<button type="button" class="confirmBtn" >' +
        '<span style="font-size: 16px;">确认</span>' +
        '</button>' +
        '<p class="tips_bottom right" id="login">登录已有账号</p>',
    _mobileHtml = '<div class="mine-btn-wrap" id="mobileDialog">' +
        '<div class="tit">' +
        '<p class="title">绑定手机号</p>' +
        '<p class="tips">初次使用快捷登录，请完善信息以创建你的账号</p>' +
        '</div>' +
        '<div class="dialog_input">' +
        '<div class="input_d"><input type="text" id="mobile" placeholder="手机号" /></div>' +
        '</div>' +
        '<div class="dialog_input">' +
        '<div class="input_d w70"><input type="text" id="verifyCode" placeholder="验证码" /></div>' +
        '<a class="verify green">获取验证码</a>' +
        '</div>' +
        '<button type="button" class="confirmBtn" >' +
        '<span style="font-size: 16px;">完成</span>' +
        '</button>' +
        '<p class="tips_bottom">点击完成代表您已阅读<a style="text-decoration:underline;color: #000;">《小清柠用户协议》</a></p>';

//检测用户是否登录
function checkLogin(successBack, failBack) {
    $.ajax({
        url: _basePath + "app/member/checkLogin",
        dataType: 'json',
        success: function (data) {
            if (typeof data !== undefined && data.resCode === 0) {
                if (typeof successBack === 'function') {
                    successBack();
                }
            } else {
                if (typeof successBack === 'function') {
                    failBack();
                }
            }
        }
    });
}

//初始化登录功能
function initLogin(callBack, params) {
    if (!$.getBrowser().versions.weixin) {
        $.myFlavr({
            contentWidth: document.documentElement.clientWidth,
            contentHeight: document.documentElement.clientHeight - 20,
            contentHtml: _loginHtml,
            onShow: function (dialog) {
                loginEvent(dialog, callBack, params);
            }
        });
    } else {
        $.myFlavr({
            contentWidth: document.documentElement.clientWidth * 0.8,
            contentHeight: 150,
            type: 2,
            contentHtml: _loginWxHtml,
            onShow: function (dialog) {
                loginWxEvent(dialog, callBack, params);
            }
        });
    }
}

//绑定手机号
function initMobile(callBack) {
    $.myFlavr({
        contentWidth: document.documentElement.clientWidth,
        contentHeight: document.documentElement.clientHeight - 20,
        contentHtml: _mobileHtml,
        onShow: function (dialog) {
            mobileEvent(dialog,callBack);
        }
    });
}

//登录事件
function loginEvent(dialog, callBack, params) {
    //登录
    $("div#loginDialog .confirmBtn").on("tap", function () {
        var pwd = $("div#loginDialog #password").val(), mobile = $("div#loginDialog #mobile").val();
        if (!isMobile(mobile)) {
            mui.toast("手机号码格式不正确");
            return false;
        }
        if (isEmpty(pwd)) {
            mui.toast("请输入密码");
            return false;
        }
        $.ajax({
            url: _basePath + "app/member/login",
            data: {
                mobile: mobile,
                password: pwd
            },
            dataType: 'json',
            success: function (data) {
                if (typeof data !== undefined && data.resCode === 0) {
                    mui.toast("登录成功");
                    dialog.hideDialog();
                    if (typeof callBack === 'function') {
                        callBack();
                    }
                } else {
                    mui.toast(data['message']);
                }
            }
        });
    });

    //微信登录
    /*mui('.bottom').on("tap","img",function () {
        window.location.href = _basePath + "app/member/loginWechat?url=" + encodeURIComponent(encodeURIComponent(location.href));
    });*/
    //切换其他登录方式
    mui('div#loginDialog .bottom').on("tap", "img", function () {
        dialog.hideDialog();
        $.myFlavr({
            contentWidth: document.documentElement.clientWidth * 0.8,
            contentHeight: 150,
            type: 2,
            contentHtml: _loginWxHtml,
            onShow: function (dialog) {
                loginWxEvent(dialog, callBack, params);
            }
        });
    });

    //跳转注册
    $("div#loginDialog #register").on("tap", function () {
        dialog.content(_registerHtml, function (dialog) {
            regEvent(dialog, callBack);
        });
    });
    //切换重置密码
    $("div#loginDialog #forget").on("tap", function () {
        dialog.content(_forgetHtml, function (dialog) {
            forgetEvent(dialog, callBack);
        });
    });
}

//微信登录事件
function loginWxEvent(dialog, callBack, params) {
    //登录
    $("div#wxLoginDialog .confirmBtn").on("tap", function () {
        var url = encodeURIComponent(location.href);
        window.location.href = _basePath + "app/member/loginWechat?url=" + url + "&loginFlag=1&params=" + encodeURIComponent(JSON.stringify(params));
    });

    //切换其他登录方式
    $("div#wxLoginDialog #otherLogin").on("tap", function () {
        dialog.hideDialog();
        $.myFlavr({
            contentWidth: document.documentElement.clientWidth,
            contentHeight: document.documentElement.clientHeight - 20,
            contentHtml: _loginHtml,
            onShow: function (dialog) {
                loginEvent(dialog, callBack, params);
            }
        });
    });

}

//注册事件
function regEvent(dialog, callBack) {
    //注册
    $("div#registerDialog .confirmBtn").on("tap", function () {
        var mobile = $("div#registerDialog #mobile").val(), pwd = $("div#registerDialog #password").val(), re_pwd = $("div#registerDialog #re_pwd").val(),
            verifyCode = $("div#registerDialog #verifyCode").val();
        if (!isMobile(mobile)) {
            mui.toast("手机号码格式不正确");
            return false;
        }
        if (isEmpty(verifyCode)) {
            mui.toast("验证码不能为空");
            return false;
        }
        if (isEmpty(pwd)) {
            mui.toast("密码不能为空");
            return false;
        }
        if (pwd !== re_pwd) {
            mui.toast("两次密码不一致");
            return false;
        }
        $.ajax({
            url: _basePath + "app/member/register",
            data: {
                mobile: mobile,
                password: pwd,
                verifyCode: verifyCode
            },
            dataType: 'json',
            success: function (data) {
                if (typeof data !== undefined && data.resCode === 0) {
                    mui.toast("注册成功");
                    dialog.hideDialog();
                    if(typeof callBack === 'function'){
                        callBack();
                    }
                } else {
                    mui.toast(data['message']);
                }
            }
        });
    });
    //发送验证码
    $("div#registerDialog .verify").on("tap", function () {
        var mobile = $("div#registerDialog #mobile").val();
        if (!isMobile(mobile)) {
            mui.toast("手机号码格式不正确");
            return false;
        }
        sendVerifyCode(mobile, "register");
    });


    //切换登录页面
    $("div#registerDialog #login").on("tap", function () {
        dialog.content(_loginHtml, function (dialog) {
            loginEvent(dialog, callBack)
        });
    });
}

//重置密码事件
function forgetEvent(dialog, callBack) {
    //重置密码
    $("div#forgetDialog .confirmBtn").on("tap", function () {
        var mobile = $("div#forgetDialog #mobile").val(), pwd = $("div#forgetDialog #password").val(), re_pwd = $("div#forgetDialog #re_pwd").val(),
            verifyCode = $("div#forgetDialog #verifyCode").val();
        if (!isMobile(mobile)) {
            mui.toast("手机号码格式不正确");
            return false;
        }
        if (isEmpty(verifyCode)) {
            mui.toast("验证码不能为空");
            return false;
        }
        if (isEmpty(pwd)) {
            mui.toast("新密码不能为空");
            return false;
        }
        if (pwd !== re_pwd) {
            mui.toast("两次密码不一致");
            return false;
        }
        $.ajax({
            url: _basePath + "app/member/pwdEdit",
            data: {
                mobile: mobile,
                pwdNew: pwd,
                verifyCode: verifyCode
            },
            dataType: 'json',
            success: function (data) {
                if (typeof data !== undefined && data.resCode === 0) {
                    mui.toast("密码修改成功");
                    dialog.hideDialog();
                    if(typeof callBack === 'function'){
                        callBack();
                    }
                } else {
                    mui.toast(data['message']);
                }
            }
        });
    });
    //发送验证码
    $("div#forgetDialog .verify").on("tap", function () {
        var mobile = $("div#forgetDialog #mobile").val();
        if (!isMobile(mobile)) {
            mui.toast("手机号码格式不正确");
            return false;
        }
        sendVerifyCode(mobile, "forget");
    });

    //切换登录页面
    $("div#forgetDialog #login").on("tap", function () {
        dialog.content(_loginHtml, function (dialog) {
            loginEvent(dialog, callBack)
        });
    });
}

//绑定手机号时间
function mobileEvent(dialog,callBack) {
    $("div#mobileDialog .verify").on("tap", function () {
        var mobile = $("div#mobileDialog #mobile").val();
        if(!isMobile(mobile)){
            mui.toast("手机号码格式不正确");
        }else{
            sendVerifyCode(mobile);
        }
    });
    function validateMobile(){
        var mobile = $('div#mobileDialog #mobile').val(),verifyCode = $('div#mobileDialog #verifyCode').val();
        if('' === $.trim(mobile)){
            mui.toast('请输入手机号！');
            return false;
        }else if(!isMobile(mobile)){
            mui.toast('手机格式不正确！');
            return false;
        }
        if('' === $.trim(verifyCode)){
            mui.toast('请输入验证码！');
            return false;
        }
        return true;
    }
    $("div#mobileDialog .confirmBtn").on("tap", function () {
        if(validateMobile()){
            $.dialog({
                titleText : '警告',
                type : 'confirm',
                buttonText : {
                    ok : '确定',
                    cancel : '取消'
                },
                onClickOk : function(){
                    bindMobile(dialog);
                },
                onClickCancel : function(){
                    return true;
                },
                contentHtml : '如果绑定的手机号已经存在，则会注销掉原来的账号，是否确定？'
            });
        }
    });
    function bindMobile(dialog) {
        $.ajax({
            url: _basePath + "app/member/bindMobile",
            data:{
                id: appLogin['member_id'],
                mobile: $('div#mobileDialog #mobile').val(),
                verifyCode: $('div#mobileDialog #verifyCode').val()
            },
            dataType:"json",
            type:"post",
            success:function(data){
                $.dialog.close();
                if(typeof data != undefined && data.resCode == 0){
                    mui.toast("绑定成功");
                    dialog.hideDialog();
                    if(typeof callBack == 'function'){
                        callBack();
                    }
                }else {
                    mui.toast(data.message);
                }
            }
        })
    }

}

//发送验证码
function sendVerifyCode(mobile, type) {
    $.ajax({
        url: _basePath + "app/member/noteCodeSend",
        data: {
            mobile: mobile,
            type: type
        },
        dataType: 'json',
        success: function (data) {
            if (typeof data != undefined && data.resCode == 0) {
                mui.toast('发送成功');
                codeTime($(".verify"))
            } else {
                mui.toast(data['message']);
            }
        }
    });
}

var wait = 60;

function codeTime(obj) {
    if (wait === 0) {
        $(obj).attr('href', 'javascript:sendVerifyCode()');
        $(obj).text("获取验证码");
        $(obj).addClass("green");
        wait = 60;
    } else {
        $(obj).attr("href", "javascript:;");
        $(obj).text("重新发送(" + wait + ")");
        $(obj).removeClass("green");
        wait--;
        setTimeout(function () {
            codeTime(obj)
        }, 1000)
    }
}

$(function () {
    if(typeof appLogin != undefined && appLogin != undefined){
        if(appLogin['needMobile'] == 1) {
            initMobile(doWxLogin);
        }else{
            doWxLogin();
        }
    }
});

function doWxLogin() {
    try {
        var isLogin = appLogin['loginFlag'];
        if (isLogin == 1) {
            var onLoadParam = JSON.parse(appLogin['params']);
            if (onLoadParam != null) {
                var func = eval(onLoadParam['onLoad']);
                if (typeof func == 'function') {
                    func(onLoadParam['data']);
                    if ($.getBrowser().versions.tbs) {
                        $.ajax({
                            url: _basePath + "/app/tbsUse",
                            data: {token: appLogin['token']},
                            dataType: 'JSON',
                            success: function () {
                                console.log("微信tbs处理多次转发")
                            }
                        })
                    }
                }
            }
        }
    } catch (e) {
        console.log(e)
    }
}
